package com.laicunba.event.listener;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.laicunba.event.UserCacheUpdateEvent;
import com.laicunba.event.inner.CacheKey;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.StringUtil;

/**
 * 用户缓存更新事件
 * 
 * @author jiagoushi
 *
 */
@Component
public class UserCacheUpdateEventListener implements ApplicationListener<UserCacheUpdateEvent> {
	private static final Logger LOG = Logger.getLogger(UserCacheUpdateEventListener.class);

	@Override
	public void onApplicationEvent(UserCacheUpdateEvent event) {
		CacheKey key = (CacheKey) event.getSource();
		try {

			if (StringUtil.isEmpty(key.getUid())) {
				return;
			}

			if (StringUtil.isEmpty(key.getKey())) {
				CacheUtil.removeAttribute("udata." + key.getUid());
			} else {
				CacheUtil.hdel("udata." + key.getUid(), key.getKey());
			}

		} catch (Exception e) {
			LOG.error("remove_cache_error", e);
		}

	}

}
